package com.example.demo.util;

import lombok.extern.slf4j.Slf4j;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author：
 * @date：
 */
@Slf4j
public class DateUtil {
    public static final long MILLISECOND = 1L;
    public static final long SECOND = 1000L;
    public static final long MINUTE;
    public static final long HOUR;
    public static final long DAY;
    public static final long YEAR;
    public static final String DATAFORMAT_YYMMDD = "yyyy-MM-dd";
    public static final String DATAFORMAT_YYMMDDHHMMSS = "yyyy-MM-dd HH:mm:ss";


    /**
     * 判断日期是否是本月的第一天
     * @param date
     * @return
     */
    public static boolean isFirstDay(Date date){
        boolean flag = false;
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
        String curr = dateFormat.format(date);
        log.info(curr);

        Calendar c = Calendar.getInstance();//可以对每个时间域单独修改

        int day = c.get(Calendar.DATE);
        if(day == 1){
            flag = true;

        }
        return flag;

    }

    //根据传入时间获取这个月第一天的零时零分零秒
    public static Date getMonthBegin(String data) {

        Date date = null;
        try {
            date = new SimpleDateFormat(DATAFORMAT_YYMMDD).parse(data);
        } catch (ParseException e) {
            log.error(e.toString());
        }

        Calendar nowTime = Calendar.getInstance();

        nowTime.setTime(date);

        nowTime.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天

        nowTime.set(Calendar.HOUR_OF_DAY, 0);//将小时至零

        nowTime.set(Calendar.MINUTE, 0);//将分钟至零

        nowTime.set(Calendar.SECOND, 0);//将秒至零

        nowTime.set(Calendar.MILLISECOND, 0);//将毫秒至零

        return nowTime.getTime();
    }
    //根据传入时间获取这个月最后一天的23时59分59秒
    public static Date getMonthEnd(String date) {

        Date data = null;
        try {
            data = new SimpleDateFormat(DATAFORMAT_YYMMDD).parse(date);
        } catch (ParseException e) {
            log.error(e.toString());
        }

        Calendar nowTime = Calendar.getInstance();

        nowTime.setTime(data);

        //设置为当月最后一天
        nowTime.set(Calendar.DAY_OF_MONTH, nowTime.getActualMaximum(Calendar.DAY_OF_MONTH));

        nowTime.set(Calendar.HOUR_OF_DAY, 23);//将小时至零

        nowTime.set(Calendar.MINUTE, 59);//将分钟至零

        nowTime.set(Calendar.SECOND, 59);//将秒至零

        nowTime.set(Calendar.MILLISECOND,999);//将毫秒至零

        return nowTime.getTime();
    }

    public static String getDateTimeString(Calendar c) {

        return getDateTimeString(c.getTime());
    }

    public static String getDateTimeString(Date date) {

        return getDateTimeString(date, DATAFORMAT_YYMMDDHHMMSS);
    }

    public static String getDateTimeStringYYYY(Date date) {

        return getDateTimeString(date, DATAFORMAT_YYMMDD);
    }

    public static String getDateTimeString(Calendar c, String format) {

        return getDateTimeString(c.getTime(), format);
    }

    public static String getDateTimeString(Date date, String format) {

        return (new SimpleDateFormat(format)).format(date);
    }

    public static String getDateString(Calendar c) {

        return getDateString(c.getTime());
    }

    public static String getDateString(Date date) {

        return getDateString(date, DATAFORMAT_YYMMDD);
    }

    public static String getDateString(Calendar c, String format) {
        return getDateString(c.getTime(), format);
    }

    public static String getDateString(Date date, String format) {
        return (new SimpleDateFormat(format)).format(date);
    }

    public static Calendar getDateTime(String str) throws ParseException {

        return getDateTime(str, DATAFORMAT_YYMMDDHHMMSS);
    }

    public static Calendar getDateTime(String str, String format) throws ParseException {

        Calendar cal = Calendar.getInstance();
        cal.setTime((new SimpleDateFormat(format)).parse(str));

        return cal;
    }

    public static Calendar getDate(String str) throws ParseException {

        return getDateTime(str, DATAFORMAT_YYMMDD);
    }

    public static Date getDate(String str, String format) throws ParseException {

        return (new SimpleDateFormat(format)).parse(str);
    }

    public static long getTimeInterval(Date before, Date later) {

        return getTimeInterval(before, later, MILLISECOND);
    }

    public static long getTimeInterval(Date before, Date later, long unit) {

        return (later.getTime() - before.getTime()) / unit;
    }

    public static Calendar getNow() {

        return Calendar.getInstance();
    }

    public static Date getNowDate() {

        return Calendar.getInstance().getTime();
    }

    public static Date getFirstSecondOfDay(Date date) {

        Calendar cal = Calendar.getInstance();
        cal.setTime(date);

        return cal.getTime();
    }

    public static Calendar getFirstSecondOfDay(Calendar cal) {

        cal.set(11, 0);
        cal.set(12, 0);
        cal.set(13, 0);
        cal.set(14, 0);

        return cal;
    }


    public static Calendar getLastSecondOfDay(Calendar cal) {

        cal.set(11, 23);
        cal.set(12, 59);
        cal.set(13, 59);
        cal.set(14, 0);

        return cal;
    }

    public static Calendar getPreviousMonthFirstDay() {

        Calendar lastDate = Calendar.getInstance();
        lastDate.add(2, -1);
        lastDate.set(5, 1);
        lastDate.set(11, 0);
        lastDate.set(12, 0);
        lastDate.set(13, 0);
        lastDate.set(14, 0);

        return lastDate;
    }

    public static Calendar getPreviousMonthLastDay() {

        Calendar lastDate = Calendar.getInstance();
        lastDate.add(2, -1);
        lastDate.set(5, 1);
        lastDate.roll(5, -1);
        lastDate.set(11, 23);
        lastDate.set(12, 59);
        lastDate.set(13, 59);
        lastDate.set(14, 0);

        return lastDate;
    }

    public static Calendar getCurrentWeekFirstDay() {

        Calendar calendar = Calendar.getInstance();
        calendar.set(7, 2);
        calendar.set(12, 0);
        calendar.set(13, 0);
        calendar.set(14, 0);

        return calendar;
    }

    public static Calendar getCurrentWeekLastDay() {

        Calendar calendar = Calendar.getInstance();
        calendar.set(7, 7);
        calendar.set(12, 0);
        calendar.set(13, 0);
        calendar.set(14, 0);

        return calendar;
    }

    public static Calendar getCurrentMonthFirstDay() {

        Calendar lastDate = Calendar.getInstance();
        lastDate.set(5, 1);
        lastDate.set(11, 0);
        lastDate.set(12, 0);
        lastDate.set(13, 0);
        lastDate.set(14, 0);

        return lastDate;
    }

    public static Calendar getCurrentMonthLastDay() {

        Calendar lastDate = Calendar.getInstance();
        lastDate.set(5, 1);
        lastDate.roll(5, -1);
        lastDate.set(11, 0);
        lastDate.set(12, 0);
        lastDate.set(13, 0);
        lastDate.set(14, 0);

        return lastDate;
    }

    public static Date getDateBefore(Date d, int unit, int value) {

        Calendar now = Calendar.getInstance();
        now.setTime(d);
        now.add(unit, -value);
        return now.getTime();

    }

    public static Date getDateAfter(Date d, int unit, int value) {

        Calendar now = Calendar.getInstance();
        now.setTime(d);
        now.add(unit, value);
        return now.getTime();

    }

    public static Date getTime(Date day, Date time) throws ParseException {

        if (day != null && time != null) {
            String timeStr = getDateTimeString(time);
            timeStr = timeStr.substring(timeStr.indexOf(" "));

            return getDateTime(getDateString(day) + timeStr).getTime();
        } else {

            return null;
        }
    }

    public static int getWeekday(Calendar calendar) {

        int weekday = calendar.get(7);
        if (weekday == 1) {
            weekday = 7;
        } else {
            --weekday;
        }

        return weekday;
    }

    public static Timestamp getTimestamp(Date date) {

        return new Timestamp(date.getTime());
    }

    public static int getAge(Date birthday, Date now) {

        Calendar cal = Calendar.getInstance();
        cal.setTime(now);
        int yearNow = cal.get(1);
        int monthNow = cal.get(2);
        int dayOfMonthNow = cal.get(5);
        cal.setTime(birthday);
        int yearBirth = cal.get(1);
        int monthBirth = cal.get(2);
        int dayOfMonthBirth = cal.get(5);
        int age = yearNow - yearBirth;
        if (monthNow <= monthBirth) {
            if (monthNow == monthBirth) {
                if (dayOfMonthNow < dayOfMonthBirth) {
                    --age;
                }
            } else {
                --age;
            }
        }

        return age;
    }


    static {

        MINUTE=SECOND*60L;
        HOUR=MINUTE*60L;
        DAY=HOUR*24L;
        YEAR=DAY*365L;
    }

    public static Calendar getLastSecondOfMonth(Calendar ca){

        ca.set(Calendar.DAY_OF_MONTH,ca.getActualMaximum(Calendar.DAY_OF_MONTH));
        return getLastSecondOfDay(ca);
    }

    public static void main(String[]args){

        Calendar calendar=Calendar.getInstance();
        calendar.set(1,2001);
        calendar.set(2,1);
        calendar.set(5,1);

    }
}
